import Mocks from "mockjs";
import { menuArr } from "../../DynamicMenuGenerate/menu";
import storage from "@/utils/storage";
const baseUrl = "/api/router";
import { getData, iresponse, paramObj } from "../index";

const newMenu = storage.get("menu") as any;

const menu = newMenu || menuArr;
let index = menu.length;

Mocks.mock(baseUrl + "/list", () => {
  return iresponse(menu);
});

Mocks.mock(baseUrl + "/add", (config) => {
  const data = getData(config);
  console.log(data);
  data.id = index++;
  menu.push(data);
  storage.set("menu", menu);
  return iresponse(data);
});
Mocks.mock(baseUrl + "/edit", (config) => {
  const data = getData(config);
  const index = menu.findIndex((v) => v.id == data.id);
  menu[index] = data;
  storage.set("menu", menu);
  return iresponse(data);
});

const delurl = /\/api\/router\/del(|\?\.*)/;
Mocks.mock(delurl, (config) => {
  const param = paramObj(config.url);
  console.log(menu);
  const index = menu.findIndex((v) => v.id == param.id);
  menu.splice(index, 1);
  storage.set("menu", menu);
  return iresponse({});
});
